// Singleton pattern.

#include <iostream>
#include <cstring>
using namespace std;

class Singleton {
	public:
		static Singleton& getInstance( ) {
			if( s_instance = NULL ) {
				s_instance = new Singleton;
			}
			++s_counter;
			return *s_instance;
		}
		void releseInstance( ) {
			if( --s_counter == 0 ) {	
				delete s_instance;
				s_instance = NULL;
			}
		}
	private:
		Singleton( ) {}
		Singleton( const Singleton& that ) {}
		static Singleton* s_instance;
		static int s_counter;
};

Singleton* Singleton::s_instance = NULL;
int Singleton::s_counter = 0;

// designer
// ------------------------------------------------------------------
// user

int main( void ) { 
	Singleton& s1 = Singleton::getInstance( );
	Singleton& s2 = Singleton::getInstance( );
	Singleton& s3 = Singleton::getInstance( );
	cout << "&s1 : " << &s1 << endl;
	cout << "&s2 : " << &s2 << endl;
	cout << "&s3 : " << &s3 << endl;
	
	s1.releaseInstance( );
	s2.releaseInstance( );
	s3.releaseInstance( );

	return 0;
}

